<?php
    //include_once 'init.php';
    if(!empty($_POST)){
        if($_FILES['url']['size']>0){
            $files=$_FILES['url'];
            $savepath=file_load($files);
        }
        $title=$_POST['title'];//标题
        $cate_id=$_POST['cate_id'];//类型
        $author=$_POST['author'];//作者
        $content=$_POST['content'];//内容
        $url=empty($savepath)?'':$savepath;//图片
        $send_user=$_SESSION['admin']['username'];//发布人
            mysqli_query($link, 'set names utf8');
            $sql="insert into `news` (`title`,`cate_id`,`author`,`content`,`send_user`,`url`) values('$title','$cate_id','$author','$content','$send_user','$url')";
            mysqli_query($link,$sql);
            $result=mysqli_affected_rows($link);
            if($result>0){
                //网页静态化
                $id=mysqli_insert_id($link);
                $constr=file_get_contents("http://127.0.0.1/cms/index.php?c=news&a=content&id=$id");
                file_put_contents("./html/$id.html", $constr);
                echo '<script>alert("新闻添加成功");location.href="index.php?c=news&a=list&admin=1";</script>';die;
            }else{
                echo '<script>alert("新闻添加失败");history.go(-1);</script>';die;
            }
    }
    $id=empty($_GET['id'])?0:$_GET['id'];
    $sql="select * from cate where pid=$id";
    $result=mysqli_query($link,$sql);
    while ($arr=mysqli_fetch_assoc($result)) {
        $cate[]=$arr;
        # code...
    }
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])){
        if($_GET['add_save']==1){
            $data['title']=$_GET['title'];
            $data['content']=$_GET['content'];
            $data['cate_id']=$_GET['cate_id'];
            $data['author']=$_GET['author'];
            $data['status']=$_GET['status'];
            $data['send_user']=$_SESSION['admin']['username'];
            $data['create_time']=date("Y-m-d H:i:s",time());
            $res=add('news',$data);
            echo $res;die;
        }
         echo json_encode($cate);die;
    }
    include(VIEW_PATH.$admin.'layout.html');
?>